﻿/****************************************************
	文件：EditorUtil.cs
	作者：HuskyT
	邮箱: 1005240602@qq.com
	日期：2021/07/03 12:52:32   	
	功能：编辑器常用工具API
*****************************************************/

using System;
using System.Text;
using UnityEditor;
using UnityEngine;

namespace HTFramework.Utility.Editor
{
    public class EditorUtil
    {
        //------------------------------  Unity编辑器相关  ------------------------------//

        #region Unity编辑器相关

        /// <summary>
        /// 复用菜单方法
        /// </summary>
        /// <param name="menuItemPath">菜单方法路径</param>
        public static void ReuseMenuItem(string menuItemPath)
        {
            EditorApplication.ExecuteMenuItem(menuItemPath);
        }

        /// <summary>
        /// 打开文件夹
        /// </summary>
        /// <param name="folderPath">文件夹路径</param>
        public static void OpenFolder(string folderPath)
        {
            Application.OpenURL("file:///" + folderPath);
            //EditorUtility.RevealInFinder(Application.persistentDataPath);
        }

        /// <summary>
        /// 将指定资源导出成UnityPackage
        /// </summary>
        /// <param name="assetPathName">待导出资源路径</param>
        /// <param name="fileName">导出文件的文件名</param>
        public static void ExportUnityPackage(string assetPathName, string fileName)
        {
            EditorUtility.DisplayProgressBar("导出当前项目", $"{Application.productName}正导出为UnityPackage",
                0.1f);
            //递归导出指定文件夹下所有文件
            AssetDatabase.ExportPackage(assetPathName, fileName, ExportPackageOptions.Recurse);
            EditorUtility.DisplayProgressBar("导出当前项目", $"{Application.productName}正导出为UnityPackage",
                1f);
            EditorUtility.ClearProgressBar();
        }
 
        /// <summary>
        /// 生成UnityPackage名
        /// 默认：项目名_日期.unitypackage
        /// </summary>
        /// <returns></returns>
        public static string GenerateUnityPackageName(string packageName = null)
        {
            if (packageName == null)
            {
                string projectName = Application.productName;
                return projectName + "_" + DateTime.Now.ToString("yyyyMMdd_HH") + ".unitypackage";
            }
            else
            {
                return packageName + "_" + DateTime.Now.ToString("yyyyMMdd_HH") + ".unitypackage";
            }
        }

        /// <summary>
        /// 打开/运行指定文件
        /// </summary>
        /// <param name="fileFullPath">文件的完整路径，包括后缀</param>
        public static void OpenFile(string fileFullPath)
        {
            System.Diagnostics.Process.Start(fileFullPath);
        }

        /// <summary>
        /// 打开当前项目
        /// </summary>
        public static void OpenCurrentProject()
        {
            EditorApplication.OpenProject(Application.dataPath.Replace("Assets", string.Empty));
        }

        #endregion
    }
}